package com.gtworldmobile.classes;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
 

import android.content.Context;
import android.widget.Toast;

public class Uploader{
	
	
	private String url="";
	private String filePath="";
	private String fileName="";
	private String IDPoi="";
	private String status="ok";
	private String statusForm="ok";
	
	private List<String> parametros;	

	private static final String FILE_CONTENT_TYPE = "image/jpeg"; 
	
    public Uploader(String url, String filePath, String fileName, List<String> parametros){
    	this.url = url;
    	this.filePath = filePath; 	
    	this.fileName = fileName;
    	this.parametros=parametros;
    }
    /*@Override
    public void run() {
    	uploadForm();
    	if(filePath!=null)
    		uploadImage();
    }*/
    
    public void uploadImage() {
    			
    	HttpClient httpClient = new DefaultHttpClient();
    	HttpPost httpPost = new HttpPost(url);
    	MultipartEntity entity = new MultipartEntity();
    	boolean fileFound=true;
    	
    	try {
    		InputStream fileContent = new BufferedInputStream(new FileInputStream(filePath));
    		entity.addPart("Foto", new InputStreamBody(fileContent, FILE_CONTENT_TYPE, fileName));

    	} catch (FileNotFoundException e) {			
			e.printStackTrace();
			setStatus("Archivo no enconctrado");	                
			fileFound=false;
		}
    	
    	if(fileFound){
    		
    		httpPost.setEntity(entity);
    		
    		try {
    			
        		httpPost.setHeader("imagen", "si");
        		httpPost.addHeader("IDPoi", IDPoi);
        		httpPost.addHeader("Nombre", parametros.get(0).toString());
        		
				HttpResponse servletResponse = httpClient.execute(httpPost);
				
				httpClient.getConnectionManager().shutdown();
				Header h = servletResponse.getLastHeader("Respuesta");
				setStatus(h.getValue());
	                
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				setStatus(e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
				setStatus(e.getMessage());
			} catch(Exception e){
				e.printStackTrace();
				setStatus(e.getMessage());
			}
    	}

    }
    
    public void uploadForm() {
    	HttpClient httpClientDat = new DefaultHttpClient();
    	HttpPost httpPostDat = new HttpPost(url);    	
    	
    	List<NameValuePair> postValues = new ArrayList<NameValuePair>(2);
    	postValues.add(new BasicNameValuePair("Nombre", parametros.get(0).toString()));         
		postValues.add(new BasicNameValuePair("Descripcion", parametros.get(1).toString()));
		postValues.add(new BasicNameValuePair("Publico", parametros.get(2).toString()));
		postValues.add(new BasicNameValuePair("URLweb", parametros.get(3).toString()));
		postValues.add(new BasicNameValuePair("Latitud", parametros.get(4).toString()));
		postValues.add(new BasicNameValuePair("Longitud", parametros.get(5).toString()));
		postValues.add(new BasicNameValuePair("Altitud", parametros.get(6).toString()));
		postValues.add(new BasicNameValuePair("IDUser", parametros.get(7).toString()));
		postValues.add(new BasicNameValuePair("TipoPoi", parametros.get(8).toString()));
		postValues.add(new BasicNameValuePair("DireccionPoi", parametros.get(9).toString()));
 
		try {
			httpPostDat.setEntity(new UrlEncodedFormEntity(postValues));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
    	
    	
    	try {
    		
    		httpPostDat.setHeader("imagen", "no");

			HttpResponse servletResponse = httpClientDat.execute(httpPostDat);

			httpClientDat.getConnectionManager().shutdown();
			
			Header h = servletResponse.getLastHeader("RespuestaDat");			
			setStatusForm(h.getValue());
			h = servletResponse.getLastHeader("IDPoi");
			IDPoi = h.getValue();
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			setStatus(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			setStatusForm(e.getMessage());
		} catch(Exception e){
			setStatusForm(e.getMessage());
		}
    	
    
    }
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
	public String getStatusForm() {
		return statusForm;
	}
	public void setStatusForm(String statusForm) {
		this.statusForm = statusForm;
	}
    
    
    
}
